package com.unknowpupil.design.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: <p>
 * 抽象构建
 * </p>
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/4/28 9:25
 * @Classname: AbstractFile
 * @Version: 1.0
 */
public interface AbstractFile {
    /**
     * 杀毒
     */
    void killVirus();
}

/**
 * 相当于叶子，leaf
 */
class ImageFile implements AbstractFile{
     private String name;

    public ImageFile(String name) {
        super();
        this.name = name;
    }

    @Override
    public void killVirus() {
        System.out.println("----图像文件----:"+name+"进行查杀");
    }
}

class TextFile implements AbstractFile{
    private String name;

    public TextFile(String name) {
        super();
        this.name = name;
    }

    @Override
    public void killVirus() {
        System.out.println("----文本文件----:"+name+"进行查杀");
    }
}
/**
 * 容器组件
 *
 */
class Folder implements AbstractFile{
    public Folder(String name) {
        super();
        this.name = name;
    }

    private String name;
    /** 定义容器  用来存放容器构建下的字节的*/
    private List<AbstractFile> list = new ArrayList<>();


    public void add(AbstractFile file){
        list.add(file);
    }

    public void remove(AbstractFile file){
        list.remove(file);
    }
    public AbstractFile getChild(int index){

      return  list.get(index);
    }

    @Override
    public void killVirus() {
        System.out.println("--文件夹："+name+" 进行查杀");
       for(AbstractFile file:list){
           //天然递归
           file.killVirus();
       }
    }
}

